<?php

namespace addons\official\model;

class OfficialCategory extends OfficialBaseModel
{

    // 表名
    protected $name = 'official_category';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';


    /**
     * 查询子级
     */
    public function son()
    {
        return $this->hasMany('official_category', 'pid', 'id')
            ->field(['content', 'e_content'], true)
            ->where('deletetime', null)
            ->order('weigh', 'desc');
    }

    /**
     * 查询父子级栏目以及某个子级栏目下的列表数据
     * @param $father_id 父级id
     * @param int $son_id 子级数量
     * @param int $son_num 子级id
     * @param int $data_num 列表数据数量
     */
    public function getFatherIsSon($father_id, $son_num = 10, $son_id = 0, $data_num = 8)
    {
        //查询父级
        $official_tree_category_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_tree_category.php';
        if (file_exists($official_tree_category_url)) {
            $tree_category_all = include $official_tree_category_url;
            foreach ($tree_category_all as $item) {
                if ($item['id'] == $father_id) {
                    $father = $item;
                }
            }
        }
        //截取几个子级栏目
        $father['son'] = array_slice($father['son'], 0, $son_num);
        //如果$son_id为0 默认查找第一个子集列表
        if ($son_id == 0) {
            foreach ($tree_category_all as $item) {
                if (isset($item['son']) && !empty($item['son'])) {
                    foreach ($item['son'] as $v) {
                        if (($v['pid'] == $father_id) && ($son_id == 0)) {
                            $son_id = $v['id'];
                        }
                    }
                }
            }
        }
        //查询数据
        $data = [];
        if ($data_num) {
            $model_name = "\addons\official\model\\" . $this->queryTableName($son_id);
            $data = (new $model_name)
                ->where('official_category_id', $son_id)
                ->where('language', $this->language)
                ->field(['content'], true)
                ->order('weigh', 'desc')
                ->limit($data_num)
                ->select();
        }
        //返回数据
        $father['data'] = $data;
        return $father;
    }


    /**
     * 查询某个栏目以及栏目下的列表数据
     * @param $c_id 栏目ID
     * @param int $data_num 数据条数
     */
    public function getOneCategoryIsData($c_id, $data_num = 8)
    {
        //1.查询父级

        $official_tree_category_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_tree_category.php';
        if (file_exists($official_tree_category_url)) {
            $tree_category_all = include $official_tree_category_url;
            foreach ($tree_category_all as $item) {
                foreach ($item['son'] as $son) {
                    if ($son['id'] == $c_id) {
                        $category = $son;
                    }
                }
                if ($item['id'] == $c_id) {
                    unset($item['son']);
                    $category = $item;
                }
            }
        }

        //4.查询数据
        $data = db($this->queryTableName($c_id))
            ->where('official_category_id', $c_id)
            ->where('language', $this->language)
            ->field(['content'], true)
            ->order('weigh', 'desc')
            ->limit($data_num)
            ->select();
        //5.返回数据
        $category['data'] = $data;
        return $category;
    }


}
